#include "stdafx.h"
#include "ServerCallbackGate.h"

ServerCallbackGate::ServerCallbackGate()
{
	RegisterHandler(0,boost::bind(&ServerCallbackGate::phPing_0, this,_1));
	RegisterHandler(1,boost::bind(&ServerCallbackGate::phPlayerLogin_1, this,_1));
	RegisterHandler(3,boost::bind(&ServerCallbackGate::phPlayerRegister_3, this,_1));
}
ServerCallbackGate::ServerCallbackGate(ISocketServer *pSocketServer)
	:m_pSocketServer(pSocketServer)
{
	RegisterHandler(0,boost::bind(&ServerCallbackGate::phPing_0, this,_1));
	RegisterHandler(1,boost::bind(&ServerCallbackGate::phPlayerLogin_1, this,_1));
	RegisterHandler(3,boost::bind(&ServerCallbackGate::phPlayerRegister_3, this,_1));
}

ServerCallbackGate::~ServerCallbackGate()
{
	
}

void ServerCallbackGate::OnRun()
{
}

void ServerCallbackGate::OnRecvData(UInt32 nSocketId, const void* pData, size_t nDataLen)
{
	PacketDecoder decoder;
	if(!decoder.SetData(reinterpret_cast<const char*>(pData), nDataLen + 4))
	{
		LOGGER_ERROR("!decoder.SetData(reinterpret_cast<const char*>(pData), nDataLen + 4)");
		return;
	}

	if(decoder.GetItemCount() < 2)
	{
		LOGGER_ERROR("decoder.GetItemCount() < 2");
		return;
	}

	Int32 type = 0;
	Int32 id = 0;

	if(!decoder.GetItem(0, type) || !decoder.GetItem(1, id))
	{
		LOGGER_ERROR("!decoder.GetItem(0, type) || !decoder.GetItem(1, id)");
		return;
	}

	#ifdef _WIN32
	if(!(type == 0 && id == 0))
		printf("ServerCallbackGate OnRecvData type:%d,id:%d\n",type,id);
	#endif

	if(id < 0 || id >= 65536)
	{
		LOGGER_ERROR("id < 0 || id >= 65536,id:%d",id);
		return;
	}

	static PacketHandlerParamGate param;
	param.nSocketId = nSocketId;
	param.pDecoder = &decoder;

	PacketHandler method = GetHandler(id);
	if(method != NULL)
	{
		method((void*)&param);
		return;
	}
	phDefault((void*)&param);
}

void ServerCallbackGate::phPing_0(void *pVoid)
{
	PacketHandlerParamGate& param = *((PacketHandlerParamGate*)pVoid);
	PacketDecoder& decoder = *param.pDecoder;
	UInt32& nSocketId = param.nSocketId;
	//LOGGER_ERROR("phPing_0,nSocketId:%d",nSocketId);
}

void ServerCallbackGate::phDefault(void *pVoid)
{
	PacketHandlerParamGate& param = *((PacketHandlerParamGate*)pVoid);
	PacketDecoder& decoder = *param.pDecoder;
	UInt32& nSocketId = param.nSocketId;

	PacketEncoder encoder;
	encoder.AddItem(Int32(nSocketId));
	EncodeData(&encoder,decoder);
	singleton<ClientCallbackGame>::instance().SendPacket(encoder);

}
void ServerCallbackGate::phPlayerLogin_1(void *pVoid)
{
	PacketHandlerParamGate& param = *((PacketHandlerParamGate*)pVoid);
	PacketDecoder& decoder = *param.pDecoder;
	UInt32& nSocketId = param.nSocketId;
	Peer* pPeer = singleton<PeerManager>::instance().Get(nSocketId);
	if (pPeer == NULL)
	{
		return;
	}
	PacketEncoder encoder;
	encoder.AddItem(Int32(nSocketId));
	EncodeData(&encoder,decoder);
	encoder.AddItem(pPeer->GetRemoteIp());
	singleton<ClientCallbackGame>::instance().SendPacket(encoder);
}
void ServerCallbackGate::phPlayerRegister_3(void *pVoid)
{
	PacketHandlerParamGate& param = *((PacketHandlerParamGate*)pVoid);
	PacketDecoder& decoder = *param.pDecoder;
	UInt32& nSocketId = param.nSocketId;
	Peer* pPeer = singleton<PeerManager>::instance().Get(nSocketId);
	if (pPeer == NULL)
	{
		return;
	}
	PacketEncoder encoder;
	encoder.AddItem(Int32(nSocketId));
	EncodeData(&encoder,decoder);
	encoder.AddItem(pPeer->GetRemoteIp());
	singleton<ClientCallbackGame>::instance().SendPacket(encoder);
}
void ServerCallbackGate::OnNewConnection(UInt32 nSocketId,const std::string& strIp)
{
	Peer* pPeer = singleton<PeerManager>::instance().Add(nSocketId);
	if (pPeer != NULL)
	{
		pPeer->SetRemoteIp(strIp);
	}
	//PacketEncoder encoder;
	//encoder.AddItem(Int32(nSocketId));
	//encoder.AddItem(Int32(0));
	//encoder.AddItem(Int32(1));
	//encoder.AddItem(strIp);
	//singleton<ClientCallbackGame>::instance().SendPacket(encoder);
	LOGGER_INFO("OnNewConnection:%d,ip:%s",nSocketId,strIp.c_str());
}

void ServerCallbackGate::OnCloseConnection(UInt32 nSocketId,const std::string& strIp)
{
	singleton<PeerManager>::instance().Remove(nSocketId);
	PacketEncoder encoder;
	encoder.AddItem(Int32(nSocketId));
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(2));
	singleton<ClientCallbackGame>::instance().SendPacket(encoder);
	LOGGER_INFO("OnCloseConnection:%d,ip:%s",nSocketId,strIp.c_str());
}

void ServerCallbackGate::OnStop()
{
}

void ServerCallbackGate::OnUpdate()
{

}

void ServerCallbackGate::SendPacket(UInt32 nSocketId,PacketEncoder& encoder)
{
	static ISocketServer* pSocketServer = GetSocketServer();
	void   *p = const_cast<void*>(encoder.GetData());
	pSocketServer->SendData(nSocketId,(char*)p+sizeof(UInt32),encoder.GetDataSize()-sizeof(UInt32));
}

void ServerCallbackGate::SendPacketToAll(PacketEncoder& encoder)
{
	singleton<PeerManager>::instance().SendPacketToAll(encoder);
}

void ServerCallbackGate::Ping(UInt32 nSocketId)
{
	static PacketEncoder encoder;
	encoder.Clear();
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(0));
	SendPacket(nSocketId,encoder);
}
//ISocketServerCallback* CreateServerCallbackGame(ISocketServer*pSocketServer)
//{
//	singleton<ServerCallbackGate>::instance().SetSocketServer(pSocketServer);
//	return &(singleton<ServerCallbackGate>::instance());
//}